package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import entidades.Processo;
import entidades.Sistema;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class PanelProcesso extends javax.swing.JPanel implements ActionListener {
	private JLabel idProcessoLabel;
	private JTextField idProcessoTxt;
	private JButton matarProcessoButton;
	private JButton addProcessoButton;
	private JTextField TempoUtiProcessoTxt;
	private JLabel tuProcessoLabel;
	private JTextField tsProcessoTxt;
	private JLabel tsProcessoLabel;

	private int id = 1;

	public PanelProcesso() {
		super();
		initGUI();
	}

	private void initGUI() {
		this.setPreferredSize(new java.awt.Dimension(205, 147));
		this.setLayout(null);
		this.setSize(205, 129);
		this.setOpaque(false);
		idProcessoLabel = new JLabel();
		this.add(idProcessoLabel);
		idProcessoLabel.setText("Identificador:");
		idProcessoLabel.setBounds(7, 17, 68, 14);
		idProcessoTxt = new JTextField();
		this.add(getIdProcessoTxt());
		idProcessoTxt.setBounds(75, 14, 113, 21);
		idProcessoTxt.setEnabled(false);
		idProcessoTxt.setText(1 + "");
		tsProcessoLabel = new JLabel();
		this.add(tsProcessoLabel);
		tsProcessoLabel.setText("\u0394TS:");
		tsProcessoLabel.setBounds(47, 48, 25, 14);
		tsProcessoTxt = new JTextField();
		this.add(getDescProcessoTxt());
		tsProcessoTxt.setBounds(75, 44, 113, 21);
		tuProcessoLabel = new JLabel();
		this.add(tuProcessoLabel);
		tuProcessoLabel.setText("\u0394TU:");
		tuProcessoLabel.setBounds(45, 76, 24, 14);
		TempoUtiProcessoTxt = new JTextField();
		this.add(TempoUtiProcessoTxt);
		TempoUtiProcessoTxt.setBounds(75, 73, 113, 21);
		addProcessoButton = new JButton();
		this.add(addProcessoButton);
		addProcessoButton.setText("Adicionar");
		addProcessoButton.setBounds(12, 102, 77, 21);
		addProcessoButton.addActionListener(this);
		matarProcessoButton = new JButton();
		this.add(matarProcessoButton);
		matarProcessoButton.setText("Excluir");
		matarProcessoButton.setBounds(119, 102, 69, 21);
		matarProcessoButton.addActionListener(this);
	}

	public JTextField getIdProcessoTxt() {
		return idProcessoTxt;
	}

	public JTextField getDescProcessoTxt() {
		return tsProcessoTxt;
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if (event.getSource().equals(addProcessoButton)) {
			if (Sistema.processos.size() < 10) {
				final String identificador = "P"
						+ Integer.parseInt(idProcessoTxt.getText());
				final int tempoS = Integer.parseInt(tsProcessoTxt.getText());
				final int tempoU = Integer.parseInt(TempoUtiProcessoTxt
						.getText());
				Processo processo = new Processo(identificador, tempoS, tempoU);
				processo.setStatus("OK");
				idProcessoTxt.setText(""+(++id));
				Sistema.processos.add(processo);
				Sistema.grafo.addVertex(processo);
				PanelLog.logTxt.setText("Processo "+identificador+" foi adicionado.\n"+PanelLog.logTxt.getText());
				Main.PosicaoProcesso();
				Main.getInstance().addProcesso(processo);
				Thread p = new Thread(processo);
				p.start();
			}else{
				JOptionPane.showMessageDialog(null,"J� Existem 10 Processos");
			}
		}else{
			if(event.getSource().equals(matarProcessoButton)){
					String id = JOptionPane.showInputDialog(
					Main.getInstance(),
					"Informe o identificador do processo a ser finalizado.",
					"Matar Processo", JOptionPane.QUESTION_MESSAGE);
					for (int i = 0; i < Sistema.processos.size(); i++) {
						if (("P"+id).equals(Sistema.processos.get(i).getIdentificador())) {
							Sistema.processos.get(i).excluir();
							PanelLog.logTxt.setText("O processo " + Sistema.processos.get(i).getIdentificador()+ " foi removido\n"+PanelLog.logTxt.getText());
							//Main.getInstance().graph.removeVertex(Sistema.processos.get(i));
							Main.graph.removeVertex(Sistema.processos.get(i));
							Sistema.grafo.removeVertex(Sistema.processos.get(i));
							Sistema.processos.remove(i);
							Sistema.verificarDeadLock();
						}
					}
			}
			
		}
	}

}
